<?php
/**
 * template.class.php
 *
 * @author          hyperblue
 * @version         0.1
 * @copyright       (C) 2011- *
 * @license	        http://www.bingceng.com
 * @lastmodify	    2011-08-11 19:30
 */

class tpl {
    
    static protected $vars = array();

    /**
     * 构造函数
     */
    public function __construct() {
	}

    /**
     * 注册变量
     */
    public function assign($name, $value) {
        $this->vars[$name] = $value;
    }

    /**
     * 显示模板
     */
    public function display($tpl_name = '', $vars = null, $module = ROUTE_M) {
		global $_config; global $_cache_config;
		if(empty($module)) $module = ROUTE_M;
        if (isset($this->vars)) extract($this->vars);
        if (is_array($vars)) extract($vars);
		
		$skin = $_config['tpl'] ? trim($_config['tpl']) : 'default';
		$suffix = $_config['tpl_suffix'] ? trim($_config['tpl_suffix']) : 'html'; 
		
		$tpl_path =  BC_TPL_PATH.$skin.DS.$module.DS;
		$tpl_name = $tpl_name ? $tpl_name : ROUTE_C.'_'.ROUTE_A;
		$tpl = $tpl_path.$tpl_name.'.'.$suffix;
		
        if (!file_exists($tpl)) die('模板没有找到!');
        
		$cache_type = $_config['cache'] ? $_config['cache'] : 'file';
		$cache_refresh = $_config['cache_refresh'];
		
		Fcache::getConfig($_cache_config[$cache_type]);
		$this->cache = Fcache::factory();
		$cache_status = $this->cache->verify($tpl_name);
		if (!$cache_status) {
			$content = @file_get_contents($tpl);
			$content = self::parse_tag($content);
			if ($this->cache->set($tpl_name, $content) === false) die('缓存生成失败');
		}
		
		$tpl = $this->cache->path($tpl_name);

		include $tpl;
    }
	
	/**
	 * 返回模板内容
	 */
	public function fetch($tpl_name, $vars = null) {
		ob_start();
		$this->display($tpl_name, $vars);
		$results = ob_get_contents();
		ob_end_clean();
		return $results;
    } 
    
    public function parse_tag($content) {
        $content = preg_replace("/\{bc:include\s+(.+)\}/", "<?php include \\1; ?>", $content);
		$content = preg_replace("/\{bc:tpl\s+(.+)\}/", "<?php if (stripos(\\1, '/') !== false) { \$a = explode('/', \\1); \$p1 = \$a[1]; \$p2 = \$a[0];} else { \$p1 = \\1; \$p2 = '';} tpl::display(\$p1, null, \$p2); ?>", $content);
		$content = preg_replace("/\{if\s+(.+?)\}/", "<?php if(\\1) { ?>", $content);
        $content = preg_replace("/\{else\}/", "<?php } else { ?>", $content);
        $content = preg_replace("/\{elseif\s+(.+?)\}/", "<?php } elseif (\\1) { ?>", $content);
        $content = preg_replace("/\{\/if\}/", "<?php } ?>", $content);
        $content = preg_replace("/\{for\s+(.+?)\}/", "<?php for(\\1) { ?>", $content);
        $content = preg_replace("/\{\/for\}/", "<?php } ?>", $content);
        $content = preg_replace("/\{bc:loop\s+(\S+)\s+(\S+)\}/", "<?php \$n=1;if(is_array(\\1)) foreach(\\1 AS \\2) { ?>", $content);
		$content = preg_replace("/\{bc:loop\s+(\S+)\s+(\S+)\s+(\S+)\}/", "<?php \$n=1; if(is_array(\\1)) foreach(\\1 AS \\2 => \\3) { ?>", $content);
		$content = preg_replace("/\{\/bc:loop\}/", "<?php \$n++;}unset(\$n); ?>", $content);
		$content = preg_replace("/\{bc:lang\s+([a-zA-Z0-9_.\/\[\]\'\"]*)\}/", "<?php echo lang::get(\\1);?>", $content);
		$content = preg_replace("/\{bc:php\s+(.+)\}/", "<?php \\1;?>", $content);
		$content = preg_replace("/\{bc:var\s+(\\$[a-zA-Z0-9_\[\]\'\"]*)\}/", "<?php echo \\1;?>", $content);
		$content = preg_replace("/\{bc:global\s+([^\{]+)\}/", "<?php echo \\1;?>", $content);
		$content = preg_replace("/\{bc:url\s+([^\{]+)\}/", "<?php echo _url(\\1);?>", $content);
		$content = preg_replace("/\{bc:module\s+([^\}]+)\}/ie", "<?php self::bc_tag('$1','$2', '$0');?>", $content);
        $content = preg_replace("/\{\/bc\}/ie", "", $content);
		
		$content = "<?php defined('BC_ROOT_PATH') or exit('No permission resources.'); ?>\r\n" . $content;
        
        return $content;
    }
	
	/**
     * 解析BC标签
     * @param string $type 操作方式
     * @param string $params 参数
     * @param string $html 匹配到的所有的HTML代码
     */
	public function bc_tag($type, $params, $html) {
	
	}
	
	/**
     * 解析语言标签
     * @param string $key 语言标识或者模块/控制器/标识
	 */
	public function bc_lang($key) {
		$this->lang = new lang;
		
		if (stripos($key, '/') !== false) { 
			$a = explode('/', $key); 
			$m = $a[0];
			$c = $a[1];
			$this->lang->load($m.'/'.$c);
			$key = $a[2];
		}
		
		return $this->lang->get($key);
	}
    
}
